package com.domain.controller;

import com.domain.common.Comments;
import com.domain.common.Const;
import com.domain.common.Result;
import com.domain.pojo.Users;
import com.domain.service.PushMessageService;
import com.domain.utils.NotNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 消息控制器
 */
@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private PushMessageService pushMessageService;

    /**
     * 获取推送消息列表
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param pushType  消息类型  1 帮忙消息  2 系统通知  3 答谢通知
     * @param pageNum   页码
     * @param pageSize  每页的长度
     * @param request
     * @return
     */
    @RequestMapping(value = "/getPushMessageList", method = RequestMethod.POST)
    @ResponseBody
    public Result getPushMessageList(String userToken, String userId, String pushType, String pageNum, String pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return pushMessageService.getPushMessageList(userId, pushType, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 修改推送消息状态（1 已读  2 未读）
     *
     * @param userToken
     * @param messageId
     * @param request
     * @return
     */
    @RequestMapping(value = "/changePushMessageState", method = RequestMethod.POST)
    @ResponseBody
    public Result changePushMessageState(String userToken, String messageId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return pushMessageService.changePushMessageState(messageId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 受理通知详情
     *
     * @param userToken     用户token
     * @param pushMessageId 推送消息id
     * @param pushType      推送类型
     * @param request
     * @return
     */
    @RequestMapping(value = "/acceptancePushDetail", method = RequestMethod.POST)
    @ResponseBody
    public Result acceptancePushDetail(String userToken, String pushMessageId, String pushType, String pushTypes, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return pushMessageService.acceptancePushDetail(pushMessageId, pushType, pushTypes);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 受理发起聊天（创建群组）
     *
     * @param userToken 用户token
     * @param usersId   用户id
     * @param friendsId 中间人id (传入json 数组 ["id","id","id"])
     * @param accpId    受理人id
     * @param accpId    publishId 发布id
     * @param request
     * @return
     */
    @RequestMapping(value = "/creatGroup", method = RequestMethod.POST)
    @ResponseBody
    public Result creatGroup(String userToken, String usersId, String friendsId, String accpId, String publishId, HttpServletRequest request) {
        //判断用户是否登录
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return pushMessageService.creatGroup(usersId, friendsId, accpId, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }

    /**
     * 邀请加入讨论组详情
     *
     * @param userToken
     * @param pushMessageId 推送消息id
     * @param pushType      推送类型（小类型）
     * @param request
     * @return
     */
    @RequestMapping(value = "/getJoinGroupDetail", method = RequestMethod.POST)
    @ResponseBody
    public Result getJoinGroupDetail(String userToken, String pushMessageId, String pushType, HttpServletRequest request) {
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return pushMessageService.getJoinGroupDetail(pushMessageId, pushType);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统失败");
        }
    }

    /**
     * 加入讨论组
     *
     * @param userToken
     * @param pushMessageId
     * @param pushType
     * @param request
     * @return
     */
    /*@RequestMapping(value = "/joinGroup", method = RequestMethod.POST)
    @ResponseBody
    public Result joinGroup(String userToken, String pushMessageId, String pushType, HttpServletRequest request) {

    }*/

}
